﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Objects;
using PsTFS.Objects.Methodes;

namespace PsTFS.Commandes.Get
{
    /// <summary>
    /// Récupération des informations sur un merge
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "MergeInfo")]
    public class Get_MergeInfo : PsCmdlet
    {
        /// <summary>
        /// Lancement du processus
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                // Get a reference to the version control service
                VersionControlServer versionControl = Methode_Service.VCS();

                List<ChangesetMerge> ci = new List<ChangesetMerge>(versionControl.QueryMerges(
                    this.SourceItemSpec,
                    this.SourceVersion != null ? this.SourceVersion : VersionSpec.Latest,
                    this.TargetItemSpec,
                    this.VersionSpec != null ? this.VersionSpec : VersionSpec.Latest,
                    this.VersionFrom,
                    this.VersionTo,
                    this.RecursionType));

                WriteObject(ci);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex,Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Get-Changeset is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS
        {
            set { Context.GetContext().Tfs = value; }
        }
        /// <summary>
        /// Element spécific
        /// </summary>
        [Parameter(Position = 1)]
        public string SourceItemSpec { get; set; }
        /// <summary>
        /// Version spécifique
        /// </summary>
        [Parameter(Position = 2)]
        public VersionSpec SourceVersion { get; set; }
        /// <summary>
        /// Element spécific
        /// </summary>
        [Parameter(Position = 3)]
        public string TargetItemSpec { get; set; }
        /// <summary>
        /// Version spécifique
        /// </summary>
        [Parameter(Position = 4)]
        public VersionSpec VersionSpec { get; set; }
        /// <summary>
        /// Version spécifique
        /// </summary>
        [Parameter(Position = 5)]
        public VersionSpec VersionFrom { get; set; }
        /// <summary>
        /// Version spécifique
        /// </summary>
        [Parameter(Position = 6)]
        public VersionSpec VersionTo { get; set; }
        /// <summary>
        /// RecursionType (true, false)
        /// </summary>
        [Parameter(Position = 7)]
        [DefaultValue(RecursionType.Full)]
        public RecursionType RecursionType { get; set; }
    }
}